package uk.ac.ncl.carrent;

import java.util.*;

/**
 * A driving licence has the driver's name (comprising a first and last name), 
 * the date of birth of the driver, a unique licence number, a date of issue, 
 * and an indication whether the licence is a full driving licence or not.
 * @author Shiwei He
 */

public final class DrivingLicence {

	private final Person person; 
	private final Calendar issueDate;
	private final LicenceNumber number;
	private final boolean isFull;
	
	/**
	 * Construct a driving licence, given a person and issue date,
	 * also indicate whether the licence is full
	 * @param person the owner of the driving licence
	 * @param String issue date of the driving lecence
	 * @param boolean the indicator of the full licence
	 */
	public DrivingLicence(Person person, String issue, boolean isFull){
		this.person = person;
		this.isFull = isFull;
		issueDate = Calendar.getInstance();
		final String parts[] = issue.split("/");
		issueDate.set(Integer.parseInt(parts[2]), Integer.parseInt(parts[1]), Integer.parseInt(parts[0]));	
		number = LicenceNumber.getInstance(this.person, issueDate, new Random().nextInt());
	}
	
	/**
	 * get the person's name in fomat firstName lastName
	 * @return String the name of the person
	 */
	public String getName(){
		return person.getFirstName() + " " + person.getLastName();
	}
	
	/**
	 * get the date of birth of the person
	 * @return Calendar the date of the person
	 */
	public Calendar getDOB(){
		return person.getDob();
	}
	
	/**
	 * get the issue date of the driving licence
	 * @return Calendar the issue date
	 */
	public Calendar getIssueDate(){
		return issueDate;
	}
	
	/**
	 * get the licence number of the driving licence
	 * @return LicenceNumber the licence number
	 */
	public LicenceNumber getLicenceNumber(){
		return number;
	}
	
	/**
	 * get the type of the driving licence,
	 * true if the licence is full,
	 * false if the licence is not full.
	 * @return boolean the type of the licence
	 */
	public boolean getType(){
		return isFull;
	}
	
	/**
	 * get the owner of the driving licence
	 * @return Person the owner
	 */
	public Person getPerson(){
		return person;
	}
	
	/**
	 * get the number of the years that the driver has got the licence
	 * @return int the age of the licence
	 */
	public int getYear(){
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.YEAR) - issueDate.get(Calendar.YEAR);
	}
	
	/**
	 * the String representation of the driving licence, with format
	 * Name: ; Date of birth: ; Issue date: ; Licence Type: ;
	 */
	public String toString(){
		return "Name: " + getName() + "; " 
				+ "Date of birth: " + person.getDob().get(Calendar.DATE) + "/" + person.getDob().get(Calendar.MONTH) + "/" + person.getDob().get(Calendar.YEAR)
				+ "Issue date: " + issueDate.get(Calendar.DATE) + issueDate.get(Calendar.MONTH) + issueDate.get(Calendar.YEAR)
				+ "Licence Type(is it full licence?): " + isFull;
	}
}

